from one_click_install_backend import *
import datetime

class TestAddLineToFileContentsIfNotMatching(unittest.TestCase):
    def testEmptyFile(self):
        currentContents = []
        expectedContents = ["blacklist my_driver\n"]
        actualContents = addLineToFileContentsIfNotMatching(r"^\s*blacklist\s+my_driver\b", "blacklist my_driver\n", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testComment(self):
        currentContents = ["#blacklist my_driver"]
        expectedContents = ["#blacklist my_driver\n", "blacklist my_driver\n"]
        actualContents = addLineToFileContentsIfNotMatching(r"^\s*blacklist\s+my_driver\b", "blacklist my_driver\n", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testOtherLineBefore(self):
        currentContents = ["blacklist zmy_driver"]
        expectedContents = ["blacklist zmy_driver\n", "blacklist my_driver\n"]
        actualContents = addLineToFileContentsIfNotMatching(r"^\s*blacklist\s+my_driver\b", "blacklist my_driver\n", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testNoReplace(self):
        currentContents = [" blacklist  my_driver #blacklisted driver\n"]
        expectedContents = [" blacklist  my_driver #blacklisted driver\n"]
        actualContents = addLineToFileContentsIfNotMatching(r"^\s*blacklist\s+my_driver\b", "blacklist my_driver\n", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
        
class TestFilterOutFileContentsMatchingLines(unittest.TestCase):
    def testEmptyFile(self):
        currentContents = []
        expectedContents = [""]
        actualContents, replaced = filterOutFileContentsMatchingLines(r"^\s*blacklist\s+my_driver\b",  currentContents)
        self.assertEqual(replaced, False)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testOneLine(self):
        currentContents = [" blacklist my_driver\n"]
        expectedContents = [""]
        actualContents, replaced = filterOutFileContentsMatchingLines(r"^\s*blacklist\s+my_driver\b",  currentContents)
        self.assertEqual(replaced, True)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testOneLineWithOther(self):
        currentContents = [" blacklist other_driver\n", " blacklist my_driver\n"]
        expectedContents = [" blacklist other_driver\n"]
        actualContents, replaced = filterOutFileContentsMatchingLines(r"^\s*blacklist\s+my_driver\b",  currentContents)
        self.assertEqual(replaced, True)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
        
class TestAddUserToPapFileContents(unittest.TestCase):
    def testEmptyFile(self):
        currentContents = []
        expectedContents = ["user * pass *\n"]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testFileWithOneOtherLine(self):
        currentContents = [
            "aaa * pass *\n"
            ]
        expectedContents = [
            "aaa * pass *\n",
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testFileWithOneOtherLineSubstring1(self):
        currentContents = [
            "use * pass *\n"
            ]
        expectedContents = [
            "use * pass *\n",
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testFileWithOneOtherLineSubstring2(self):
        currentContents = [
            "user2 * pass *\n"
            ]
        expectedContents = [
            "user2 * pass *\n",
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testFileWithOneOtherLineNoNewline(self):
        currentContents = [
            "user2 * pass *"
            ]
        expectedContents = [
            "user2 * pass *\n",
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testCommentedOutUser(self):
        currentContents = [
            "#user * pass *\n"
            ]
        expectedContents = [
            "#user * pass *\n",
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testPasswordReplace(self):
        currentContents = [
            "user * pass1 *\n"
            ]
        expectedContents = [
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testPasswordReplace2(self):
        currentContents = [
            " user * pass1 *"
            ]
        expectedContents = [
            "\nuser * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testPasswordReplaceWithQuotes1(self):
        currentContents = [
            "\"user\" * \"pass1\" *\n"
            ]
        expectedContents = [
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testPasswordReplaceWithQuotes2(self):
        currentContents = [
            "'user' * 'pass1' *\n"
            ]
        expectedContents = [
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testPasswordReplaceWithTabs(self):
        currentContents = [
            "\t\tuser\t*\tpass1\t*\t\t\n"
            ]
        expectedContents = [
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testPasswordReplaceInTheMiddle(self):
        currentContents = [
            "user1 * pass1 *\n",
            "user * pass1 *\n",
            "use * pass1 *\n"
            ]
        expectedContents = [
            "user1 * pass1 *\n",
            "use * pass1 *\n"
            "user * pass *\n",
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testPasswordReplaceWithComments(self):
        currentContents = [
            "#aaaa\n",
            "user * pass1 *\n",
            "#blah\n"
            ]
        expectedContents = [
            "#aaaa\n",
            "#blah\n",
            "user * pass *\n"
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))
    def testPasswordAppendWithComments(self):
        currentContents = [
            "#aaaa\n",
            "user2 * pass1 *\n",
            "#blah\n"
            ]
        expectedContents = [
            "#aaaa\n",
            "user2 * pass1 *\n",
            "#blah\n",
            "user * pass *\n",
            ]
        actualContents = addUserToPapFileContents("user", "pass", currentContents)
        self.assertEqual("".join(actualContents), "".join(expectedContents))

class TestMakeFileBackup(unittest.TestCase):
    def setUp(self):
        import tempfile
        self.testDir = tempfile.mkdtemp()
        self.fileName = self.testDir + "/origFile"
        file = open(self.fileName, "w")
        self.fileContents = "sdfsdfndndd\nssas"
        file.write(self.fileContents)
        file.close()
    def testMakingBackups(self):
        maxBackups = 200
        #Hm... might not work around midnight...
        backupBaseName = self.fileName + "." + datetime.date.today().isoformat()
        for i in range(1, maxBackups):
            makeFileBackup(self.fileName)
        for i in range(1, maxBackups):
            backupName = backupBaseName + "." + str(i)
            self.assert_(os.path.exists(backupName))
            file = open(backupName, "r")
            currentContents = file.read()
            self.assertEqual(self.fileContents, currentContents)
            file.close()
        #Check if not too many were created
        backupName = backupBaseName + "." + str(maxBackups + 1)
        self.assert_(os.path.exists(backupName) == False)
        #Check if backup with zero was not created
        backupName = backupBaseName + "." + str(0)
        self.assert_(os.path.exists(backupName) == False)
        #Check if creating too many raises exception
        self.assertRaises(Exception, makeFileBackup, self.fileName)
    def tearDown(self):
        shutil.rmtree(self.testDir)

if __name__ == '__main__':
    unittest.main()
